/**
 * 游戏全局配置中心
 * 
 * 职责：
 * 1. 统一管理所有游戏配置
 * 2. 提供类型安全的配置访问
 * 3. 支持开发/生产环境切换
 * 4. 便于调试和调优
 * 
 * 使用方式：
 * import { GameConfig } from '../config/GameConfig';
 * 
 * const fps = GameConfig.Performance.LOGIC_FPS;
 * const layers = GameConfig.Scene.DEFAULT_LAYERS;
 * 
 * @author AI Assistant
 * @date 2025-11-27
 */

import { LogLevel } from '../core/logger/Logger';
import { LayerConfig } from '../scripts/coordinator/SceneCoordinator';

// ============================================================================
// 性能配置
// ============================================================================

/**
 * 性能相关配置
 */
export const PerformanceConfig = {
    /** 逻辑系统帧率（FPS）*/
    LOGIC_FPS: 20 as number,
    
    /** 渲染系统帧率（FPS）*/
    RENDER_FPS: 60 as number,
    
    /** 最大单帧时间（秒）- 防止死亡螺旋 */
    MAX_DELTA_TIME: 0.25 as number,
    
    /** 最大跳帧数 */
    MAX_FRAME_SKIP: 5 as number,
    
    /** 是否启用插值渲染 */
    ENABLE_INTERPOLATION: true as boolean,
    
    /** 是否启用性能统计 */
    ENABLE_PERFORMANCE_STATS: true as boolean,
    
    /** 统计信息打印间隔（毫秒）*/
    STATS_PRINT_INTERVAL: 5000 as number,
    
    /** 是否显示 FPS 统计 */
    SHOW_FPS_STATS: true as boolean,
};

// ============================================================================
// 场景配置
// ============================================================================

/**
 * 场景相关配置
 */
export const SceneConfig = {
    /** 默认场景层级配置 */
    DEFAULT_LAYERS: [
        { name: 'background', zIndex: -100, description: '背景层' },
        { name: 'game', zIndex: 0, description: '游戏对象层' },
        { name: 'effect', zIndex: 100, description: '特效层' },
        { name: 'ui', zIndex: 200, description: 'UI层' },
        { name: 'top', zIndex: 300, description: '顶层（调试等）' }
    ] as LayerConfig[],
    
    /** 场景预加载列表 */
    PRELOAD_SCENES: ['Main', 'Battle'] as string[],
    
    /** 是否启用场景缓存 */
    ENABLE_SCENE_CACHE: true as boolean,
};

// ============================================================================
// 日志配置
// ============================================================================

/**
 * 日志系统配置
 */
export const LoggerConfig = {
    /** 是否启用颜色 */
    ENABLE_COLOR: true as boolean,
    
    /** 是否显示时间戳 */
    ENABLE_TIMESTAMP: true as boolean,
    
    /** 是否显示调用栈（警告和错误） */
    ENABLE_STACK_TRACE: true as boolean,
    
    /** 默认日志级别 */
    DEFAULT_LEVEL: LogLevel.DEBUG as LogLevel,
    
    /** 是否启用性能统计 */
    ENABLE_PERFORMANCE_STATS: true as boolean,
    
    /** 日志历史记录大小 */
    MAX_HISTORY_SIZE: 1000 as number,
    
    /** 是否启用批量日志 */
    ENABLE_BATCHING: false as boolean,
    
    /** 批量日志刷新间隔（毫秒）*/
    BATCH_FLUSH_INTERVAL: 1000 as number,
};

// ============================================================================
// ECS 配置
// ============================================================================

/**
 * ECS 系统配置
 */
export const ECSConfig = {
    /** 实体池初始大小 */
    ENTITY_POOL_SIZE: 100 as number,
    
    /** 组件池初始大小 */
    COMPONENT_POOL_SIZE: 500 as number,
    
    /** 是否启用统计 */
    ENABLE_STATISTICS: true as boolean,
    
    /** 是否启用调试模式 */
    DEBUG_MODE: true as boolean,
    
    /** 是否预热对象池 */
    WARMUP_POOLS: true as boolean,
    
    /** 预热实体数量 */
    WARMUP_ENTITY_COUNT: 50 as number,
    
    /** 预热组件数量 */
    WARMUP_COMPONENT_COUNT: 200 as number,
};

// ============================================================================
// 资源配置
// ============================================================================

/**
 * 资源管理配置
 */
export const ResourceConfig = {
    /** 预制体缓存大小 */
    PREFAB_CACHE_SIZE: 50 as number,
    
    /** 纹理缓存大小 */
    TEXTURE_CACHE_SIZE: 100 as number,
    
    /** 音频缓存大小 */
    AUDIO_CACHE_SIZE: 30 as number,
    
    /** 是否启用资源预加载 */
    ENABLE_PRELOAD: true as boolean,
    
    /** 预加载超时时间（毫秒）*/
    PRELOAD_TIMEOUT: 30000 as number,
    
    /** 资源加载重试次数 */
    MAX_RETRY_COUNT: 3 as number,
    
    /** 重试延迟（毫秒）*/
    RETRY_DELAY: 1000 as number,
    
    /** 是否启用资源版本控制 */
    ENABLE_VERSIONING: true as boolean,
};

// ============================================================================
// 节点池配置
// ============================================================================

/**
 * 节点池配置
 */
export const NodePoolConfig = {
    /** 是否启用统计 */
    ENABLE_STATS: true as boolean,
    
    /** 默认池大小 */
    DEFAULT_POOL_SIZE: 20 as number,
    
    /** 最大池大小 */
    MAX_POOL_SIZE: 100 as number,
    
    /** 预创建数量 */
    PRELOAD_COUNT: 10 as number,
    
    /** 是否自动清理 */
    AUTO_CLEANUP: true as boolean,
    
    /** 清理间隔（毫秒）*/
    CLEANUP_INTERVAL: 60000 as number,
};

// ============================================================================
// 命令队列配置
// ============================================================================

/**
 * 命令队列配置
 */
export const CommandQueueConfig = {
    /** 是否启用依赖排序 */
    ENABLE_DEPENDENCY_SORT: true as boolean,
    
    /** 最大执行超时（毫秒）*/
    MAX_EXECUTION_TIMEOUT: 100 as number,
    
    /** 命令历史记录大小 */
    MAX_HISTORY_SIZE: 100 as number,
    
    /** 是否启用命令缓存 */
    ENABLE_COMMAND_CACHE: true as boolean,
    
    /** 默认命令优先级 */
    DEFAULT_PRIORITY: 0 as number,
};

// ============================================================================
// 网络配置
// ============================================================================

/**
 * 网络相关配置
 */
export const NetworkConfig = {
    /** 服务器地址 */
    SERVER_URL: 'ws://localhost:8080' as string,
    
    /** 连接超时（毫秒）*/
    CONNECT_TIMEOUT: 5000 as number,
    
    /** 心跳间隔（毫秒）*/
    HEARTBEAT_INTERVAL: 30000 as number,
    
    /** 重连间隔（毫秒）*/
    RECONNECT_INTERVAL: 3000 as number,
    
    /** 最大重连次数 */
    MAX_RECONNECT_COUNT: 5 as number,
    
    /** 消息队列大小 */
    MESSAGE_QUEUE_SIZE: 100 as number,
};

// ============================================================================
// 调试配置
// ============================================================================

/**
 * 调试相关配置
 */
export const DebugConfig = {
    /** 是否启用调试模式 */
    ENABLE_DEBUG: true as boolean,
    
    /** 是否显示 FPS */
    SHOW_FPS: true as boolean,
    
    /** 是否显示实体数量 */
    SHOW_ENTITY_COUNT: true as boolean,
    
    /** 是否显示内存使用 */
    SHOW_MEMORY_USAGE: true as boolean,
    
    /** 是否显示碰撞盒 */
    SHOW_COLLISION_BOX: false as boolean,
    
    /** 是否启用热重载 */
    ENABLE_HOT_RELOAD: true as boolean,
    
    /** 是否启用 GM 命令 */
    ENABLE_GM_COMMANDS: true as boolean,
};

// ============================================================================
// 游戏规则配置
// ============================================================================

/**
 * 游戏规则配置
 */
export const GameRuleConfig = {
    /** 玩家初始生命值 */
    PLAYER_INITIAL_HP: 100 as number,
    
    /** 玩家初始等级 */
    PLAYER_INITIAL_LEVEL: 1 as number,
    
    /** 玩家移动速度 */
    PLAYER_MOVE_SPEED: 200 as number,
    
    /** 敌人生成间隔（秒）*/
    ENEMY_SPAWN_INTERVAL: 3 as number,
    
    /** 最大敌人数量 */
    MAX_ENEMY_COUNT: 50 as number,
    
    /** 经验值倍率 */
    EXP_MULTIPLIER: 1.0 as number,
    
    /** 金币掉落概率 */
    GOLD_DROP_RATE: 0.5 as number,
};

// ============================================================================
// 音频配置
// ============================================================================

/**
 * 音频配置
 */
export const AudioConfig = {
    /** 主音量（0-1）*/
    MASTER_VOLUME: 1.0 as number,
    
    /** 背景音乐音量（0-1）*/
    BGM_VOLUME: 0.6 as number,
    
    /** 音效音量（0-1）*/
    SFX_VOLUME: 0.8 as number,
    
    /** 是否启用音频 */
    ENABLE_AUDIO: true as boolean,
    
    /** 是否循环播放背景音乐 */
    BGM_LOOP: true as boolean,
    
    /** 最大同时播放音效数 */
    MAX_CONCURRENT_SFX: 16 as number,
};

// ============================================================================
// 统一配置导出
// ============================================================================

/**
 * 游戏全局配置
 * 
 * 将所有配置模块整合到一个对象中，便于访问和管理
 */
export const GameConfig = {
    /** 性能配置 */
    Performance: PerformanceConfig,
    
    /** 场景配置 */
    Scene: SceneConfig,
    
    /** 日志配置 */
    Logger: LoggerConfig,
    
    /** ECS 配置 */
    ECS: ECSConfig,
    
    /** 资源配置 */
    Resource: ResourceConfig,
    
    /** 节点池配置 */
    NodePool: NodePoolConfig,
    
    /** 命令队列配置 */
    CommandQueue: CommandQueueConfig,
    
    /** 网络配置 */
    Network: NetworkConfig,
    
    /** 调试配置 */
    Debug: DebugConfig,
    
    /** 游戏规则配置 */
    GameRule: GameRuleConfig,
    
    /** 音频配置 */
    Audio: AudioConfig,
};

// ============================================================================
// 类型导出
// ============================================================================

/** 导出配置类型 */
export type GameConfigType = typeof GameConfig;
export type PerformanceConfigType = typeof PerformanceConfig;
export type SceneConfigType = typeof SceneConfig;
export type LoggerConfigType = typeof LoggerConfig;
export type ECSConfigType = typeof ECSConfig;
export type ResourceConfigType = typeof ResourceConfig;

// ============================================================================
// 使用示例
// ============================================================================

/**
 * 使用示例：
 * 
 * ```typescript
 * import { GameConfig } from '../config/GameConfig';
 * 
 * // 访问性能配置
 * const logicFPS = GameConfig.Performance.LOGIC_FPS;
 * 
 * // 访问场景配置
 * const layers = GameConfig.Scene.DEFAULT_LAYERS;
 * 
 * // 访问日志配置
 * const logLevel = GameConfig.Logger.DEFAULT_LEVEL;
 * 
 * // 在系统初始化时使用
 * fixedUpdateManager.configure({
 *     logicFPS: GameConfig.Performance.LOGIC_FPS,
 *     maxDeltaTime: GameConfig.Performance.MAX_DELTA_TIME,
 *     enableInterpolation: GameConfig.Performance.ENABLE_INTERPOLATION
 * });
 * 
 * sceneCoordinator.init(sceneRoot, GameConfig.Scene.DEFAULT_LAYERS);
 * 
 * logger.configure({
 *     level: GameConfig.Logger.DEFAULT_LEVEL,
 *     enableColor: GameConfig.Logger.ENABLE_COLOR,
 *     showTimestamp: GameConfig.Logger.ENABLE_TIMESTAMP
 * });
 * ```
 */

